package org.dea.mapping;


import java.io.File;
import java.net.URL;
import java.util.Map;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.*;
import static org.junit.Assert.*;


public class EasyAcceptMappingReaderTest {

	String DIRECTORY = System.getProperty( "user.dir" ) + File.separator + "test" + File.separator + "xml";


	@Before
	public void setUp() throws Exception {
		if( !DIRECTORY.startsWith("/") )  //widows fix
			DIRECTORY = "/"+DIRECTORY;
		
	}


	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testProcessMappingFile() throws Exception {
		
		

		String mappingFilePath = DIRECTORY + File.separator + "test4_mapping.xml";
		Map componentList = EasyAcceptMappingReader.processMappingFile( mappingFilePath );
		DistributedComponent cpm = (DistributedComponent) componentList.get( "alpha" );
		assertEquals( "alpha", cpm.getHostLabel() );
		assertEquals( "nemo.lsd.ufcg.edu.br", cpm.getHostAddress() );
		assertEquals( 1099, cpm.getHostPort() );
		assertEquals( "org.ourbackup.facade.OurBackupFacade", cpm.getFacade() );
		assertEquals( "ourbackup.jar", cpm.getJarPath() );
		assertEquals( "./test/acceptTests/properties/alpha.properties", cpm.getArgs() );

		cpm = (DistributedComponent) componentList.get( "beta" );
		assertEquals( "beta", cpm.getHostLabel() );
		assertEquals( "ourico.lsd.ufcg.edu.br", cpm.getHostAddress() );
		assertEquals( 1099, cpm.getHostPort() );
		assertEquals( "org.ourbackup.facade.OurBackupFacade", cpm.getFacade() );
		assertEquals( "ourbackup.jar", cpm.getJarPath() );
		assertEquals( "ourico.lsd.ufcg.edu.br", cpm.getHostAddress() );
		assertEquals( "./test/acceptTests/properties/beta.properties", cpm.getArgs() );

	}

	@Test(expected=Exception.class)
	public void testAtLeastOneElmentExcecption2() throws Exception {
		String mappingFilePath = DIRECTORY + File.separator + "test1_mapping.xml";
		EasyAcceptMappingReader.processMappingFile( mappingFilePath );
	}

	@Test(expected=Exception.class)
	public void testAtLeastOneElmentExcecption() throws Exception {
		String mappingFilePath = DIRECTORY + File.separator + "test3_mapping.xml";
		EasyAcceptMappingReader.processMappingFile( mappingFilePath );
	}

	@Test(expected=Exception.class)
	public void testDuplicateElementException() throws Exception {
		String mappingFilePath = DIRECTORY + File.separator + "test2_mapping.xml";
		EasyAcceptMappingReader.processMappingFile( mappingFilePath );
	}

}
